/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controler;

import Bean.Evaluacion;
import Bean.Views.Ejercicio;
import DataBase.TransacionException;
import Services.Calificador;
import Services.ResultadoService;
import Util.Constantes;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Eduardo
 */
public class EvaluatorController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                       
              response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CodigoListaControler</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet CodigoListaControler at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ArrayList<Ejercicio> ejercicios = (ArrayList<Ejercicio>) request.getSession().getAttribute("evaluacion");
        ArrayList<String> respuestas = new ArrayList<String>();
        for(int count = 0; count < Constantes.NUMERO_EJERCICIOS; count++){
            respuestas.add(request.getParameter(count + ""));
        }
        Calificador calif = new Calificador();
        int calific=calif.calificar(ejercicios, respuestas);

        Evaluacion evaluacion = new Evaluacion();
        evaluacion.setAciertos(calif.getNumAciertos());
        evaluacion.setCalificacion(calific);
        evaluacion.setErrores(calif.getNumErrores());
        evaluacion.setIdAlumno((String)request.getSession().getAttribute("idAlumno"));
        evaluacion.setNumAcDrag(calif.getNumAciertosDrag());
        evaluacion.setNumAcPreg(calif.getNumAciertosPreguntas());
        evaluacion.setNumAcSin(calif.getNumAciertosSintaxis());
        evaluacion.setTotal(calif.getNumPreguntas());
        ResultadoService guardado=new ResultadoService();
        try {
            guardado.guardarResultadoEval(evaluacion);
        } catch (TransacionException ex) {
            ex.getMessage();
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        System.out.println("POST: " + request.getParameter("0"));
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
